<!DOCTYPE HTML>
<html>

<head>
	<link rel="bookmark"  type="image/x-icon"  href="/img/logo_miccall.png"/>
	<link rel="shortcut icon" href="/img/logo_miccall.png">
	
			    <title>
    Ame雨
    </title>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
    <link rel="stylesheet" href="/amelin-ting/css/mic_main.css" />
    <link rel="stylesheet" href="/amelin-ting/css/dropdownMenu.css" />
    <meta name="keywords" content="Ame雨" />
    
    	<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
	 
    <noscript>
        <link rel="stylesheet" href="/amelin-ting/css/noscript.css" />
    </noscript>
    <style type="text/css">
        body:before {
          content: ' ';
          position: fixed;
          top: 0;
          background: url('/amelin-ting/img/bg.jpg') center 0 no-repeat;
          right: 0;
          bottom: 0;
          left: 0;
          background-size: cover; 
        }
    </style>

			    
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script async type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


    <script src="/amelin-ting/js/jquery.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrollex.min.js"></script>
    <script src="/amelin-ting/js/jquery.scrolly.min.js"></script>
    <script src="/amelin-ting/js/skel.min.js"></script>
    <script src="/amelin-ting/js/util.js"></script>
    <script src="/amelin-ting/js/main.js"></script>
	
<meta name="generator" content="Hexo 5.4.0"></head>
    
		
<!-- Layouts -->



<!--  代码渲染  -->
<link rel="stylesheet" href="/css/prism_coy.css" />
<link rel="stylesheet" href="/css/typo.css" />
<!-- 文章页 -->
<body class="is-loading">
    <!-- Wrapper 外包 s-->
    <div id="wrapper" class="fade-in">
        <!-- Intro 头部显示 s -->
        <!-- Intro 头部显示 e -->
        <!-- Header 头部logo start -->
        <header id="header">
    <a href="/amelin-ting/" class="logo">Art Space</a>
</header>
        <!-- Nav 导航条 start -->
        <nav id="nav" class="special" >
            <ul class="menu links" >
			<!-- Homepage  主页  --> 
			<li >
	            <a href="/amelin-ting/" rel="nofollow">主页</a>
	        </li>
			<!-- categories_name  分类   --> 
	        
	        <li class="active">
	            <a href="#s1">分类</a>
	                    <ul class="submenu">
	                        <li>
	                        <a class="category-link" href="/amelin-ting/categories/Go/">Go</a></li><li><a class="category-link" href="/amelin-ting/categories/django/">django</a></li><li><a class="category-link" href="/amelin-ting/categories/drf/">drf</a></li><li><a class="category-link" href="/amelin-ting/categories/gin/">gin</a></li><li><a class="category-link" href="/amelin-ting/categories/go/">go</a></li><li><a class="category-link" href="/amelin-ting/categories/linux/">linux</a></li><li><a class="category-link" href="/amelin-ting/categories/mysql/">mysql</a></li><li><a class="category-link" href="/amelin-ting/categories/python/">python</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%86%85%E7%BD%91%E7%A9%BF%E9%80%8F/">内网穿透</a></li><li><a class="category-link" href="/amelin-ting/categories/%E5%89%8D%E7%AB%AF/">前端</a>
	                    </ul>
	        </li>
	        
	        <!-- archives  归档   --> 
	        
	        
		        <!-- Pages 自定义   -->
		        
		        <li>
		            <a href="/about/" title="简历">
		                简历
		            </a>
		        </li>
		        
		        <li>
		            <a href="/group/" title="团队">
		                团队
		            </a>
		        </li>
		        
		        <li>
		            <a href="/gallery/" title="图库">
		                图库
		            </a>
		        </li>
		        
		        <li>
		            <a href="/tag/" title="标签">
		                标签
		            </a>
		        </li>
		        


            </ul>
            <!-- icons 图标   -->
			<ul class="icons">
                    
                    <li>
                        <a title="github" href="https://github.com/miccall" target="_blank" rel="noopener">
                            <i class="icon fa fa-github"></i>
                        </a>
                    </li>
                    
                    <li>
                        <a title="500px" href="http://500px.com" target="_blank" rel="noopener">
                            <i class="icon fa fa-500px"></i>
                        </a>
                    </li>
                    
			</ul>
</nav>

        <div id="main" >
            <div class ="post_page_title_img" style="height: 25rem;background-image: url(https://img1.baidu.com/it/u=1267481129,2167677532&amp;fm=26&amp;fmt=auto);background-position: center; background-repeat:no-repeat; background-size:cover;-moz-background-size:cover;overflow:hidden;" >
                <a href="#" style="padding: 4rem 4rem 2rem 4rem ;"><h2 >drf路由与认证</h2></a>
            </div>
            <!-- Post -->
            <div class="typo" style="padding: 3rem;">
                <h2 id="一、路由"><a href="#一、路由" class="headerlink" title="一、路由"></a>一、路由</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 路由有三种配置方法</span></span><br><span class="line">	<span class="number">1.</span> 在urls.py中配置</span><br><span class="line">        url(<span class="string">&#x27;book/&#x27;</span>, views.BookView.as_view()), <span class="comment"># django1.x</span></span><br><span class="line">        url(<span class="string">&#x27;book/(?P&lt;pk&gt;\d+)&#x27;</span>, views.Book4DetailView.as_view()), <span class="comment"># django1.x</span></span><br><span class="line">        path(<span class="string">&#x27;books4/&#x27;</span>, views.Book4View.as_view()), <span class="comment"># django2.x</span></span><br><span class="line">        re_path(<span class="string">&#x27;books4/(?P&lt;pk&gt;\d+)&#x27;</span>, views.Book4DetailView.as_view()), <span class="comment"># django2.x</span></span><br><span class="line">        </span><br><span class="line">	<span class="number">2.</span> 一旦视图类，继承了VewSetMixin，路由</span><br><span class="line">    	path(<span class="string">&#x27;books/&#x27;</span>, views.BookView.as_view(actions=&#123;<span class="string">&#x27;get&#x27;</span>: <span class="string">&#x27;list&#x27;</span>, <span class="string">&#x27;post&#x27;</span>, <span class="string">&#x27;create&#x27;</span>&#125;)) </span><br><span class="line">        <span class="comment"># 当路径匹配，又是get请求，会执行BookViews的list方法</span></span><br><span class="line">   		re_path(<span class="string">&#x27;books/(P&lt;pk&gt;\d+)&#x27;</span>,views.BookView.as_view(</span><br><span class="line">actions&#123;<span class="string">&#x27;get&#x27;</span>:<span class="string">&#x27;retrieve&#x27;</span>,<span class="string">&#x27;put&#x27;</span>:<span class="string">&#x27;update&#x27;</span>,<span class="string">&#x27;delete&#x27;</span>:<span class="string">&#x27;destroy&#x27;</span>&#125;)),</span><br><span class="line">            </span><br><span class="line">	<span class="number">3.</span> 继承自视图类，ModelViewSet的路由写法(自动生成路由)</span><br><span class="line">        <span class="comment"># urls.py</span></span><br><span class="line">            第一步：导入routers模块</span><br><span class="line">                <span class="keyword">from</span> rest_framework <span class="keyword">import</span> routers</span><br><span class="line">            第二步：有两个类，实例化得到对象</span><br><span class="line">                routers.DefaultRouter <span class="comment"># 生成的路由有6个</span></span><br><span class="line">                routers.SimpleRoter <span class="comment"># 生成路由有2个</span></span><br><span class="line">            第三步：注册</span><br><span class="line">                router.register(<span class="string">&#x27;前缀&#x27;</span>, <span class="string">&#x27;继承自ModerViewSet视图类&#x27;</span>, <span class="string">&#x27;别名&#x27;</span>)</span><br><span class="line">                router.regitser(<span class="string">&#x27;books&#x27;</span>, views.BookViewSet) <span class="comment"># 不用加斜杠了</span></span><br><span class="line">            第四步</span><br><span class="line">                router.urls <span class="comment"># 自动生成路由 加入到原路由</span></span><br><span class="line">                urlpatterns += router.urls</span><br><span class="line">        <span class="comment"># views.py</span></span><br><span class="line">            <span class="keyword">from</span> rest_framework.viewsets <span class="keyword">import</span> ModelViewSet</span><br><span class="line">            <span class="keyword">from</span> app01.models <span class="keyword">import</span> Book</span><br><span class="line">            <span class="keyword">from</span> app01.ser <span class="keyword">import</span> BookSerializer</span><br><span class="line">            <span class="class"><span class="keyword">class</span> <span class="title">BookViewSet</span>(<span class="params">ModelViewSet</span>):</span></span><br><span class="line">                queryset =Book.objects</span><br><span class="line">                serializer_class = BookSerializer</span><br></pre></td></tr></table></figure>

<h3 id="1-1-action的使用"><a href="#1-1-action的使用" class="headerlink" title="1.1 action的使用"></a>1.1 action的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1.</span> action是干什么的</span><br><span class="line">	为了给继承自ModelViewSet的视图类中定义的函数也添加路由</span><br><span class="line">    </span><br><span class="line"><span class="number">2.</span> 代码演示</span><br><span class="line">	<span class="keyword">from</span> rest_framework.decorators <span class="keyword">import</span> action</span><br><span class="line">	<span class="class"><span class="keyword">class</span> <span class="title">BookViewSet</span>(<span class="params">ModelViewSet</span>):</span></span><br><span class="line">        query = Book.objects.<span class="built_in">all</span>()</span><br><span class="line">        serializer_class = BookSerializer</span><br><span class="line">        <span class="string">&quot;&quot;&quot;</span></span><br><span class="line"><span class="string">        methods第一个参数，传一个列表，列表中放请求方式，</span></span><br><span class="line"><span class="string">        ^books/get_1/$ [name=&#x27;book-get-1&#x27;] 当向这个地址 发送get请求，会执行下面的函数</span></span><br><span class="line"><span class="string">        detail：布尔类型 如果是True</span></span><br><span class="line"><span class="string">        ^books/(?P&lt;pk&gt;[^/.]+)/get_1/$ [name=&#x27;book-get-1&#x27;]</span></span><br><span class="line"><span class="string">        &quot;&quot;&quot;</span></span><br><span class="line"><span class="meta">        @action(<span class="params">methods=[<span class="string">&#x27;GET&#x27;</span>,<span class="string">&#x27;POST&#x27;</span>], detail=<span class="literal">False</span></span>)</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">get_1</span>(<span class="params">self, request</span>)</span></span><br><span class="line"><span class="function">            <span class="title">book</span>=<span class="title">self</span>.<span class="title">get_queryset</span>()[:</span><span class="number">2</span>]</span><br><span class="line">            ser=self.get_serializer(book, many=<span class="literal">True</span>)</span><br><span class="line">            <span class="keyword">return</span> Response(ser.data)</span><br><span class="line"></span><br><span class="line"><span class="number">3.</span> 装饰器</span><br><span class="line">	放在被装饰的函数上方，method:请求方式， detail:是否带pk</span><br></pre></td></tr></table></figure>

<h2 id="二、认证"><a href="#二、认证" class="headerlink" title="二、认证"></a>二、认证</h2><h3 id="2-1-认证的写法"><a href="#2-1-认证的写法" class="headerlink" title="2.1 认证的写法"></a>2.1 认证的写法</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 认证的实现步骤</span></span><br><span class="line">	<span class="number">1.</span>写一个类，继承BaseAuthentication，重写authenticate，认证的逻辑写在里面，认证通过，返回两个值，一个值最终给了Request对象的user， 认证失败，抛异常:APIException或者AuthenticationFailed</span><br><span class="line">	<span class="number">2.</span> 全局使用，局部使用</span><br></pre></td></tr></table></figure>

<h3 id="2-2-认证的源码分析"><a href="#2-2-认证的源码分析" class="headerlink" title="2.2 认证的源码分析"></a>2.2 认证的源码分析</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#1 APIVIew----》dispatch方法---》self.initial(request, *args, **kwargs)----&gt;有认证，权限，频率</span></span><br><span class="line"><span class="comment">#2 只读认证源码： self.perform_authentication(request)</span></span><br><span class="line"><span class="comment">#3 self.perform_authentication(request)就一句话：request.user，需要去drf的Request对象中找user属性（方法） </span></span><br><span class="line"><span class="comment">#4 Request类中的user方法，刚开始来，没有_user,走 self._authenticate()</span></span><br><span class="line"></span><br><span class="line"><span class="comment">#5 核心，就是Request类的 _authenticate(self):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">_authenticate</span>(<span class="params">self</span>):</span></span><br><span class="line">        <span class="comment"># 遍历拿到一个个认证器，进行认证</span></span><br><span class="line">        <span class="comment"># self.authenticators配置的一堆认证类产生的认证类对象组成的 list</span></span><br><span class="line">        <span class="comment">#self.authenticators 你在视图类中配置的一个个的认证类：authentication_classes=[认证类1，认证类2]，对象的列表</span></span><br><span class="line">        <span class="keyword">for</span> authenticator <span class="keyword">in</span> self.authenticators:</span><br><span class="line">            <span class="keyword">try</span>:</span><br><span class="line">                <span class="comment"># 认证器(对象)调用认证方法authenticate(认证类对象self, request请求对象)</span></span><br><span class="line">                <span class="comment"># 返回值：登陆的用户与认证的信息组成的 tuple</span></span><br><span class="line">                <span class="comment"># 该方法被try包裹，代表该方法会抛异常，抛异常就代表认证失败</span></span><br><span class="line">                user_auth_tuple = authenticator.authenticate(self) <span class="comment">#注意这self是request对象</span></span><br><span class="line">            <span class="keyword">except</span> exceptions.APIException:</span><br><span class="line">                self._not_authenticated()</span><br><span class="line">                <span class="keyword">raise</span></span><br><span class="line"></span><br><span class="line">            <span class="comment"># 返回值的处理</span></span><br><span class="line">            <span class="keyword">if</span> user_auth_tuple <span class="keyword">is</span> <span class="keyword">not</span> <span class="literal">None</span>:</span><br><span class="line">                self._authenticator = authenticator</span><br><span class="line">                <span class="comment"># 如何有返回值，就将 登陆用户 与 登陆认证 分别保存到 request.user、request.auth</span></span><br><span class="line">                self.user, self.auth = user_auth_tuple</span><br><span class="line">                <span class="keyword">return</span></span><br><span class="line">        <span class="comment"># 如果返回值user_auth_tuple为空，代表认证通过，但是没有 登陆用户 与 登陆认证信息，代表游客</span></span><br><span class="line">        self._not_authenticated()</span><br></pre></td></tr></table></figure>

<h3 id="2-3-认证组件的使用"><a href="#2-3-认证组件的使用" class="headerlink" title="2.3  认证组件的使用"></a>2.3  认证组件的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 写一个认证类 app_auth.py</span></span><br><span class="line">    <span class="keyword">from</span> rest_framework.authentication <span class="keyword">import</span> BaseAuthentication</span><br><span class="line">    <span class="keyword">from</span> rest_framework.exceptions <span class="keyword">import</span> AuthenticationFailed</span><br><span class="line">    <span class="keyword">from</span> app01.models <span class="keyword">import</span> UserToken</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">MyAuthentication</span>(<span class="params">BaseAuthentication</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">Authentication</span>(<span class="params">self, request</span>):</span></span><br><span class="line">            <span class="comment"># 认证逻辑，如果认证通过，返回两个值</span></span><br><span class="line">            <span class="comment">#如果认证失败，抛出AuthenticationFailed异常</span></span><br><span class="line">            token=request.GET.get(<span class="string">&#x27;token&#x27;</span>)</span><br><span class="line">            <span class="keyword">if</span>  token:</span><br><span class="line">                user_token=UserToken.objects.<span class="built_in">filter</span>(token=token).first()</span><br><span class="line">                <span class="comment"># 认证通过</span></span><br><span class="line">                <span class="keyword">if</span> user_token:</span><br><span class="line">                    <span class="keyword">return</span> user_token.user,token</span><br><span class="line">                <span class="keyword">else</span>:</span><br><span class="line">                    <span class="keyword">raise</span> AuthenticationFailed(<span class="string">&#x27;认证失败&#x27;</span>)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                <span class="keyword">raise</span> AuthenticationFailed(<span class="string">&#x27;请求地址中需要携带token&#x27;</span>)</span><br><span class="line"></span><br><span class="line"><span class="comment"># views.py</span></span><br><span class="line">    <span class="keyword">import</span> uuid</span><br><span class="line">    <span class="keyword">from</span> rest_framework.viewsets <span class="keyword">import</span> ModelViewSet</span><br><span class="line">    <span class="keyword">from</span> app01 <span class="keyword">import</span> models, ser, utils</span><br><span class="line">    <span class="keyword">from</span> rest_framework.decorators <span class="keyword">import</span> action</span><br><span class="line">    <span class="keyword">from</span> rest_framework.response <span class="keyword">import</span> Response</span><br><span class="line">    <span class="keyword">from</span> rest_framework.views <span class="keyword">import</span> APIView</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">LoginView</span>(<span class="params">APIView</span>):</span></span><br><span class="line">        authentication_classes = []</span><br><span class="line"></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">post</span>(<span class="params">self, request</span>):</span></span><br><span class="line">            response_dir = utils.MyResponse()</span><br><span class="line">            username = request.data.get(<span class="string">&#x27;username&#x27;</span>)</span><br><span class="line">            password = request.data.get(<span class="string">&#x27;password&#x27;</span>)</span><br><span class="line">            user_obj = models.User.objects.<span class="built_in">filter</span>(username=username, password=password).first()</span><br><span class="line">            <span class="keyword">if</span> user_obj:</span><br><span class="line">                token = uuid.uuid4()</span><br><span class="line">                models.UserToken.objects.update_or_create(defaults=&#123;<span class="string">&#x27;token&#x27;</span>: token&#125;, user=user_obj)</span><br><span class="line">                response_dir.token = token</span><br><span class="line">                <span class="keyword">return</span> Response(response_dir.get_response)</span><br><span class="line">            <span class="keyword">else</span>:</span><br><span class="line">                response_dir.msg = <span class="string">&#x27;用户名密码错误&#x27;</span></span><br><span class="line">                <span class="keyword">return</span> Response(response_dir.get_response)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 可以有多个认证，从左到右依次执行</span></span><br><span class="line"><span class="comment"># 全局使用，在setting.py中配置</span></span><br><span class="line">    REST_FRAMEWORK=&#123;</span><br><span class="line">        <span class="string">&quot;DEFAULT_AUTHENTICATION_CLASSES&quot;</span>:[<span class="string">&quot;app01.app_auth.MyAuthentication&quot;</span>,]</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment"># 局部使用，在视图类上写</span></span><br><span class="line">	authentication_classes=[MyAuthentication]</span><br><span class="line"><span class="comment"># 局部禁用</span></span><br><span class="line">	authentication_classes=[]</span><br></pre></td></tr></table></figure>

<h2 id="权限"><a href="#权限" class="headerlink" title="权限"></a>权限</h2><h3 id="权限的源码分析"><a href="#权限的源码分析" class="headerlink" title="权限的源码分析"></a>权限的源码分析</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># APIView----&gt;dispatch----&gt;initial---&gt;self.check_permissions(request)(APIView的对象方法)</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">check_permissions</span>(<span class="params">self, request</span>):</span></span><br><span class="line">        <span class="comment"># 遍历权限对象列表得到一个个权限对象(权限器)，进行权限认证</span></span><br><span class="line">        <span class="keyword">for</span> permission <span class="keyword">in</span> self.get_permissions():</span><br><span class="line">            <span class="comment"># 权限类一定有一个has_permission权限方法，用来做权限认证的</span></span><br><span class="line">            <span class="comment"># 参数：权限对象self、请求对象request、视图类对象</span></span><br><span class="line">            <span class="comment"># 返回值：有权限返回True，无权限返回False</span></span><br><span class="line">            <span class="keyword">if</span> <span class="keyword">not</span> permission.has_permission(request, self):</span><br><span class="line">                self.permission_denied(</span><br><span class="line">                    request, message=<span class="built_in">getattr</span>(permission, <span class="string">&#x27;message&#x27;</span>, <span class="literal">None</span>)</span><br><span class="line">                )</span><br></pre></td></tr></table></figure>

<h3 id="权限的使用"><a href="#权限的使用" class="headerlink" title="权限的使用"></a>权限的使用</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 写一个类，继承BasePermission,重写has_permission，如果权限通过，就返回True,不通过就返回False</span></span><br><span class="line"><span class="keyword">from</span> rest_framework.permissions <span class="keyword">import</span> BasePermission</span><br><span class="line"></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">UserPermission</span>(<span class="params">BasePermission</span>):</span></span><br><span class="line">    <span class="function"><span class="keyword">def</span>  <span class="title">has_permission</span>(<span class="params">self, request, view</span>):</span></span><br><span class="line">        <span class="comment"># 不是超级用户，不能访问</span></span><br><span class="line">        <span class="comment"># 由于认证已经过了，request内就有user对象了，当前登录用户</span></span><br><span class="line">        user=request.user  <span class="comment"># 当前登录用户</span></span><br><span class="line">        <span class="comment"># 如果该字段用了choice，通过get_字段名_display()就能取出choice后面的中文</span></span><br><span class="line">        <span class="built_in">print</span>(user.get_user_type_display())</span><br><span class="line">        <span class="keyword">if</span> user.user_type==<span class="number">1</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">True</span></span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">False</span></span><br><span class="line">        </span><br><span class="line"><span class="comment"># 局部使用</span></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">TestView</span>(<span class="params">APIView</span>):</span></span><br><span class="line">        permission_classes = [app_auth.UserPermission]</span><br><span class="line"><span class="comment"># 全局使用</span></span><br><span class="line">    REST_FRAMEWORK=&#123;</span><br><span class="line">        <span class="string">&quot;DEFAULT_AUTHENTICATION_CLASSES&quot;</span>:[<span class="string">&quot;app01.app_auth.MyAuthentication&quot;</span>,],</span><br><span class="line">        <span class="string">&#x27;DEFAULT_PERMISSION_CLASSES&#x27;</span>: [</span><br><span class="line">            <span class="string">&#x27;app01.app_auth.UserPermission&#x27;</span>,</span><br><span class="line">        ],</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment"># 局部禁用</span></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">TestView</span>(<span class="params">APIView</span>):</span></span><br><span class="line">        permission_classes = []</span><br></pre></td></tr></table></figure>

<h3 id="内置权限"><a href="#内置权限" class="headerlink" title="内置权限"></a>内置权限</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 演示一下内置权限的使用: IsAdminUser,控制是否对网站后台有权限的人</span></span><br><span class="line"><span class="number">1.</span> 创建超级管理员</span><br><span class="line"></span><br><span class="line"><span class="number">2.</span> 写一个测试视图类</span><br><span class="line">    <span class="keyword">from</span> rest_framework.permissions <span class="keyword">import</span> IsAdminUser</span><br><span class="line">    <span class="keyword">from</span> rest_framework.authentication <span class="keyword">import</span> SessionAuthentication</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">TestView3</span>(<span class="params">APIView</span>):</span></span><br><span class="line">        authentication_classes=[SessionAuthentication,]</span><br><span class="line">        permission_classes = [IsAdminUser]</span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">get</span>(<span class="params">self,request,*args,**kwargs</span>):</span></span><br><span class="line">            <span class="keyword">return</span> Response(<span class="string">&#x27;这是22222222测试数据，超级管理员可以看&#x27;</span>)</span><br><span class="line">        </span><br><span class="line"><span class="number">3.</span> 超级用户登陆到admin,在访问test3就有权限</span><br><span class="line"></span><br><span class="line"><span class="number">4.</span> 正常的话，普通管理员，没有权限看(判断的是auth_user表里的is_staff字段)</span><br></pre></td></tr></table></figure>

<h2 id="频率"><a href="#频率" class="headerlink" title="频率"></a>频率</h2><h3 id="内置的频率限制-限制未登录用户"><a href="#内置的频率限制-限制未登录用户" class="headerlink" title="内置的频率限制(限制未登录用户)"></a>内置的频率限制(限制未登录用户)</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 全局使用  限制未登录用户1分钟访问5次</span></span><br><span class="line">    REST_FRAMEWORK = &#123;</span><br><span class="line">        <span class="string">&#x27;DEFAULT_THROTTLE_CLASSES&#x27;</span>: (</span><br><span class="line">            <span class="string">&#x27;rest_framework.throttling.AnonRateThrottle&#x27;</span>,</span><br><span class="line">        ),</span><br><span class="line">        <span class="string">&#x27;DEFAULT_THROTTLE_RATES&#x27;</span>: &#123;</span><br><span class="line">            <span class="string">&#x27;anon&#x27;</span>: <span class="string">&#x27;3/m&#x27;</span>,</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line"><span class="comment">##############views.py</span></span><br><span class="line">    <span class="keyword">from</span> rest_framework.permissions <span class="keyword">import</span> IsAdminUser</span><br><span class="line">    <span class="keyword">from</span> rest_framework.authentication <span class="keyword">import</span> SessionAuthentication,BasicAuthentication</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">TestView4</span>(<span class="params">APIView</span>):</span></span><br><span class="line">        authentication_classes=[]</span><br><span class="line">        permission_classes = []</span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">get</span>(<span class="params">self,request,*args,**kwargs</span>):</span></span><br><span class="line">            <span class="keyword">return</span> Response(<span class="string">&#x27;我是未登录用户&#x27;</span>)</span><br><span class="line"></span><br><span class="line">    <span class="comment"># 局部使用</span></span><br><span class="line">    <span class="keyword">from</span> rest_framework.permissions <span class="keyword">import</span> IsAdminUser</span><br><span class="line">    <span class="keyword">from</span> rest_framework.authentication <span class="keyword">import</span> SessionAuthentication,BasicAuthentication</span><br><span class="line">    <span class="keyword">from</span> rest_framework.throttling <span class="keyword">import</span> AnonRateThrottle</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">TestView5</span>(<span class="params">APIView</span>):</span></span><br><span class="line">        authentication_classes=[]</span><br><span class="line">        permission_classes = []</span><br><span class="line">        throttle_classes = [AnonRateThrottle]</span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">get</span>(<span class="params">self,request,*args,**kwargs</span>):</span></span><br><span class="line">            <span class="keyword">return</span> Response(<span class="string">&#x27;我是未登录用户，TestView5&#x27;</span>)</span><br></pre></td></tr></table></figure>

<h3 id="内置频率限制之限制登录用户的访问频次"><a href="#内置频率限制之限制登录用户的访问频次" class="headerlink" title="内置频率限制之限制登录用户的访问频次"></a>内置频率限制之限制登录用户的访问频次</h3><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 需求：未登录用户1分钟访问5次，登录用户一分钟访问10次</span></span><br><span class="line">全局：在setting中</span><br><span class="line">      <span class="string">&#x27;DEFAULT_THROTTLE_CLASSES&#x27;</span>: (</span><br><span class="line">            <span class="string">&#x27;rest_framework.throttling.AnonRateThrottle&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;rest_framework.throttling.UserRateThrottle&#x27;</span></span><br><span class="line">        ),</span><br><span class="line">        <span class="string">&#x27;DEFAULT_THROTTLE_RATES&#x27;</span>: &#123;</span><br><span class="line">            <span class="string">&#x27;user&#x27;</span>: <span class="string">&#x27;10/m&#x27;</span>,</span><br><span class="line">            <span class="string">&#x27;anon&#x27;</span>: <span class="string">&#x27;5/m&#x27;</span>,</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">局部配置：</span><br><span class="line">    在视图类中配一个就行	</span><br></pre></td></tr></table></figure>

<h2 id="过滤"><a href="#过滤" class="headerlink" title="过滤"></a>过滤</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="number">1</span> 安装：pip3 install django-<span class="built_in">filter</span></span><br><span class="line"><span class="number">2</span> 注册，在app中注册</span><br><span class="line"><span class="number">3</span> 全局配，或者局部配</span><br><span class="line">     <span class="string">&#x27;DEFAULT_FILTER_BACKENDS&#x27;</span>: (<span class="string">&#x27;django_filters.rest_framework.DjangoFilterBackend&#x27;</span>,)</span><br><span class="line"><span class="number">4</span> 视图类</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">BookView</span>(<span class="params">ListAPIView</span>):</span></span><br><span class="line">        queryset = Book.objects.<span class="built_in">all</span>()</span><br><span class="line">        serializer_class = BookSerializer</span><br><span class="line">        filter_fields = (<span class="string">&#x27;name&#x27;</span>,)  <span class="comment"># 配置可以按照哪个字段来过滤，如果是两个，则其一满足就显示出来</span></span><br></pre></td></tr></table></figure>

<h2 id="排序"><a href="#排序" class="headerlink" title="排序"></a>排序</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 局部使用和全局使用</span></span><br><span class="line"><span class="comment"># 局部使用</span></span><br><span class="line">    <span class="keyword">from</span> rest_framework.generics <span class="keyword">import</span> ListAPIView</span><br><span class="line">    <span class="keyword">from</span> rest_framework.filters <span class="keyword">import</span> OrderingFilter, SearchFilter</span><br><span class="line">    <span class="keyword">from</span> app01.models <span class="keyword">import</span> Book</span><br><span class="line">    <span class="keyword">from</span> app01.ser <span class="keyword">import</span> BookSerializer</span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">Book2View</span>(<span class="params">ListAPIView</span>):</span></span><br><span class="line">        queryset = Book.objects.<span class="built_in">all</span>()</span><br><span class="line">        serializer_class = BookSerializer</span><br><span class="line">        filter_backends = [OrderingFilter]</span><br><span class="line">        ordering_fields = (<span class="string">&#x27;id&#x27;</span>, <span class="string">&#x27;price&#x27;</span>)</span><br><span class="line">    </span><br><span class="line"><span class="comment"># urls.py</span></span><br><span class="line">    path(<span class="string">&#x27;books2/&#x27;</span>, views.Book2View.as_view()),</span><br><span class="line">    ]</span><br><span class="line"></span><br><span class="line"><span class="comment"># 使用：</span></span><br><span class="line">    http://<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">8000</span>/books2/?ordering=-price</span><br><span class="line">    http://<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">8000</span>/books2/?ordering=price</span><br><span class="line">    http://<span class="number">127.0</span><span class="number">.0</span><span class="number">.1</span>:<span class="number">8000</span>/books2/?ordering=-<span class="built_in">id</span>	</span><br></pre></td></tr></table></figure>

<h2 id="异常处理"><a href="#异常处理" class="headerlink" title="异常处理"></a>异常处理</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">#统一接口返回</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># 自定义异常方法，替换掉全局</span></span><br><span class="line"><span class="comment"># 写一个方法</span></span><br><span class="line"><span class="comment"># 自定义异常处理的方法</span></span><br><span class="line">    <span class="keyword">from</span> rest_framework.views <span class="keyword">import</span> exception_handler</span><br><span class="line">    <span class="keyword">from</span> rest_framework.response <span class="keyword">import</span> Response</span><br><span class="line">    <span class="keyword">from</span> rest_framework <span class="keyword">import</span> status</span><br><span class="line">    <span class="function"><span class="keyword">def</span> <span class="title">my_exception_handler</span>(<span class="params">exc, context</span>):</span></span><br><span class="line">        response=exception_handler(exc, context)</span><br><span class="line">        <span class="comment"># 两种情况，一个是None，drf没有处理</span></span><br><span class="line">        <span class="comment">#response对象，django处理了，但是处理的不符合咱们的要求</span></span><br><span class="line">        <span class="comment"># print(type(exc))</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">if</span> <span class="keyword">not</span> response:</span><br><span class="line">            <span class="keyword">if</span> <span class="built_in">isinstance</span>(exc, ZeroDivisionError):</span><br><span class="line">                <span class="keyword">return</span> Response(data=&#123;<span class="string">&#x27;status&#x27;</span>: <span class="number">777</span>, <span class="string">&#x27;msg&#x27;</span>: <span class="string">&quot;除以0的错误&quot;</span> + <span class="built_in">str</span>(exc)&#125;, status=status.HTTP_400_BAD_REQUEST)</span><br><span class="line">            <span class="keyword">return</span> Response(data=&#123;<span class="string">&#x27;status&#x27;</span>:<span class="number">999</span>,<span class="string">&#x27;msg&#x27;</span>:<span class="built_in">str</span>(exc)&#125;,status=status.HTTP_400_BAD_REQUEST)</span><br><span class="line">        <span class="keyword">else</span>:</span><br><span class="line">            <span class="comment"># return response</span></span><br><span class="line">            <span class="keyword">return</span> Response(data=&#123;<span class="string">&#x27;status&#x27;</span>:<span class="number">888</span>,<span class="string">&#x27;msg&#x27;</span>:response.data.get(<span class="string">&#x27;detail&#x27;</span>)&#125;,status=status.HTTP_400_BAD_REQUEST)</span><br><span class="line"></span><br><span class="line"><span class="comment"># 全局配置setting.py</span></span><br><span class="line">	<span class="string">&#x27;EXCEPTION_HANDLER&#x27;</span>: <span class="string">&#x27;app01.app_auth.my_exception_handler&#x27;</span>,</span><br><span class="line"></span><br></pre></td></tr></table></figure>

<h2 id="封装Response"><a href="#封装Response" class="headerlink" title="封装Response"></a>封装Response</h2><figure class="highlight python"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 以后都用自己封装的</span></span><br><span class="line">    <span class="class"><span class="keyword">class</span> <span class="title">APIResponse</span>(<span class="params">Response</span>):</span></span><br><span class="line">        <span class="function"><span class="keyword">def</span> <span class="title">__init__</span>(<span class="params">self,code=<span class="number">100</span>,msg=<span class="string">&#x27;成功&#x27;</span>,data=<span class="literal">None</span>,status=<span class="literal">None</span>,headers=<span class="literal">None</span>,**kwargs</span>):</span></span><br><span class="line">            dic = &#123;<span class="string">&#x27;code&#x27;</span>: code, <span class="string">&#x27;msg&#x27;</span>: msg&#125;</span><br><span class="line">            <span class="keyword">if</span>  data:</span><br><span class="line">                dic = &#123;<span class="string">&#x27;code&#x27;</span>: code, <span class="string">&#x27;msg&#x27;</span>: msg,<span class="string">&#x27;data&#x27;</span>:data&#125;</span><br><span class="line">            dic.update(kwargs)</span><br><span class="line">            <span class="built_in">super</span>().__init__(data=dic, status=status,headers=headers)</span><br><span class="line"><span class="comment"># views.py使用</span></span><br><span class="line">    <span class="keyword">return</span> APIResponse(data=&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&#x27;lqz&#x27;</span>&#125;,token=<span class="string">&#x27;dsafsdfa&#x27;</span>,aa=<span class="string">&#x27;dsafdsafasfdee&#x27;</span>)</span><br><span class="line">    <span class="keyword">return</span> APIResponse(data=&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&#x27;lqz&#x27;</span>&#125;)</span><br><span class="line">    <span class="keyword">return</span> APIResponse(code=<span class="string">&#x27;101&#x27;</span>,msg=<span class="string">&#x27;错误&#x27;</span>,data=&#123;<span class="string">&quot;name&quot;</span>:<span class="string">&#x27;lqz&#x27;</span>&#125;,token=<span class="string">&#x27;dsafsdfa&#x27;</span>,aa=<span class="string">&#x27;dsafdsafasfdee&#x27;</span>,header=&#123;&#125;)</span><br></pre></td></tr></table></figure>

<p>莱卡棉？</p>

            </div>

            <!-- Post Comments -->
            
    <!-- 使用 DISQUS_CLICK -->
<div id="disqus-comment">
    <div id="disqus_thread"></div>

<!-- add animation -->
<style>
	.disqus_click_btn {
            line-height: 30px;
            margin: 0;
            min-width: 50px;
            padding: 0 14px;
            display: inline-block;
            font-family: "Roboto", "Helvetica", "Arial", sans-serif;
            font-size: 14px;
            font-weight: 400;
            text-transform: uppercase;
            letter-spacing: 0;
            overflow: hidden;
            will-change: box-shadow;
            transition: box-shadow .2s cubic-bezier(.4, 0, 1, 1), background-color .2s cubic-bezier(.4, 0, .2, 1), color .2s cubic-bezier(.4, 0, .2, 1);
            outline: 0;
            cursor: pointer;
            text-decoration: none;
            text-align: center;
            vertical-align: middle;
            border: 0;
            background: rgba(158, 158, 158, .2);
            box-shadow: 0 2px 2px 0 rgba(0, 0, 0, .14), 0 3px 1px -2px rgba(0, 0, 0, .2), 0 1px 5px 0 rgba(0, 0, 0, .12);
            color: #fff;
            background-color: #7EC0EE;
            text-shadow: 0
        }
</style>
	
<div class="btn_click_load" id="disqus_bt"> 
    <button class="disqus_click_btn">点击查看评论</button>
</div>

<!--
<script type="text/javascript">
$('.btn_click_load').click(function() {
    /* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
    var disqus_shortname = 'http-miccall-tech'; // required: replace example with your forum shortname

    /* * * DON'T EDIT BELOW THIS LINE * * */
    (function() {
      var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
      dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
      (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
    })();

    document.getElementById('disqus_bt').style.display = "none";
});
</script>
-->
<script type="text/javascript">
    var disqus_config = function () {
        this.page.url = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/25/drf------%E8%B7%AF%E7%94%B1&amp;%E8%AE%A4%E8%AF%81/';  // Replace PAGE_URL with your page's canonical URL variable
        this.page.identifier = 'https://gitee.com/AmeX/amelin-ting.git/2018/05/25/drf------%E8%B7%AF%E7%94%B1&amp;%E8%AE%A4%E8%AF%81/'; // Replace PAGE_IDENTIFIER with your page's unique identifier variable
    };
</script>

<script type="text/javascript">
    $('.btn_click_load').click(function() {  //click to load comments
        (function() { // DON'T EDIT BELOW THIS LINE
            var d = document;
            var s = d.createElement('script');
            s.src = '//http-miccall-tech.disqus.com/embed.js';
            s.setAttribute('data-timestamp', + new Date());
            (d.head || d.body).appendChild(s);
        })();
        $('.btn_click_load').css('display','none');
    });
</script>
</div>
<style>
    #disqus-comment{
        background-color: #eee;
        padding: 2pc;
    }
</style>


        </div>
        <!-- Copyright 版权 start -->
                <div id="copyright">
            <ul>
                <li>&copy;Powered By <a target="_blank" rel="noopener" href="https://hexo.io/zh-cn/" style="border-bottom: none;">hexo</a></li>
                <li>Design: <a target="_blank" rel="noopener" href="http://miccall.tech " style="border-bottom: none;">miccall</a></li>
            </ul>
            
                <span id="busuanzi_container_site_pv">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
			
        </div>
    </div>
</body>



 	
</html>
